package 二叉树;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/
 */
public class _783_二叉搜索树节点最小距离 {
    int pre;
    int ans;
    public int minDiffInBST(TreeNode root) {
        pre = -1;
        ans = Integer.MAX_VALUE;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        dfs(node.left);
        if (pre != -1) {
            ans = Math.min(Math.abs(pre - node.val), ans);
        }
        pre = node.val;
        dfs(node.right);
    }
}
